package edu.hawaii.halealohacli;

import java.util.GregorianCalendar;
import java.util.List;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import org.wattdepot.client.WattDepotClient;
import org.wattdepot.resource.source.jaxb.Source;

/**
 * DailyEnergy Class, which runs though finding the right tower name and energy.
 * 
 * @author John MacMillan
 * @author Mitchell Kupfer
 * @author Bryson Hori
 */
public class DailyEnergy {
  WattDepotClient client;
  protected String tower;
  protected String time;

  /**
   * Constructor for the DailyEnergy class.
   * 
   * @param tower the tower we want to query.
   * @param time the day we want to get energy for.
   * @param client the WattDepot client.
   */
  public DailyEnergy(String tower, String time, WattDepotClient client) {
    this.client = client;
    this.tower = tower;
    this.time = time;
  }

  /**
   * Simple accessor method to get the value of tower or the tower name.
   * 
   * @return Returns the value of tower or tower name.
   */
  public String getTower() {
    return this.tower;
  }

  /**
   * Simple accessor method to get the value of time.
   * 
   * @return Returns the value of time.
   */
  public String getTime() {
    return this.time;
  }

  /**
   * Runs the actual command.
   * 
   * @throws Exception If error occurs while running the method.
   * @return a boolean, true if command is successful.
   */
  public boolean run() throws Exception {
    boolean value = false;
    List<Source> sources = this.client.getSources();
    String sourceName;
    double energy;
    int year = Integer.parseInt(time.substring(0, 4));
    int month = Integer.parseInt(time.substring(5, 7));
    int day = Integer.parseInt(time.substring(8, 10));

    // Creates a start time to find the energy consumed.
    GregorianCalendar calendar = new GregorianCalendar();
    calendar.set(GregorianCalendar.YEAR, year);
    calendar.set(GregorianCalendar.MONTH, month - 1);
    calendar.set(GregorianCalendar.DAY_OF_MONTH, day);
    calendar.set(GregorianCalendar.HOUR_OF_DAY, 0);
    calendar.set(GregorianCalendar.MINUTE, 0);
    calendar.set(GregorianCalendar.SECOND, 0);
    XMLGregorianCalendar startTime =
        DatatypeFactory.newInstance().newXMLGregorianCalendar(calendar);
    // Creates the end time to find the energy consumed.
    calendar.set(GregorianCalendar.HOUR_OF_DAY, 23);
    calendar.set(GregorianCalendar.MINUTE, 59);
    calendar.set(GregorianCalendar.SECOND, 59);
    XMLGregorianCalendar endTime = DatatypeFactory.newInstance().newXMLGregorianCalendar(calendar);

    // Goes threw all sources looking for the correct tower name.
    for (Source source : sources) {
      sourceName = source.getName();
      // Finds the correct tower name.
      if (sourceName.compareTo(this.tower) == 0) {
        // gets the energy consumed based on tower name and time.
        energy = client.getEnergyConsumed(sourceName, startTime, endTime, 15);
        // print the corresponding data based on tower name and data received.
        System.out.format("%s's energy consumption for %s was: %s kWh\n", sourceName, this.time,
            energy / 1000);
        value = true;
      }
    }

    return value;
  }
}
